package com.hu.hutool.controller;


import com.commons.tools.page.PageData;
import com.commons.tools.utils.Result;
import com.hu.hutool.ThreadUtil.ThreadUtilTest;
import com.hu.hutool.dto.UserDto;
import com.hu.hutool.dto.vo.UserVo;
import com.hu.hutool.entity.User;
import com.hu.hutool.service.IUserService;
import com.hu.hutool.util.I18nUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author CazCen
 * @Date 2021/10/24 22:27
 */

@RestController
@RequestMapping("/test/info")
@Api(tags = "用户信息")
@Slf4j
public class UserController extends BaseController {


    @Resource
    IUserService iUserService;
    
    /**
     *
     */

    @PostMapping("get/user/info")
    @ApiOperation("获取用户信息")
    public Result<UserDto> getUserInfo(@RequestBody UserVo user) {


        // 国际化测试(切换浏览器语言)
        if (user.getAge() != null && user.getAge() < 18) {
            String ageLessThan18 = I18nUtil.getString("ageLessThan18");

            log.info("ageLessThan18:{}", ageLessThan18);

            String ageLessThan181 = I18nUtil.getStringByBundle("ageLessThan18");

            log.info("ageLessThan181:{}", ageLessThan181);

            return Result.error(ageLessThan181);
        }

        UserDto oneUser = iUserService.getOneUser(user);
        return Result.ok(oneUser);
    }


    @PostMapping("set/user/info")
    @ApiOperation("添加用户信息")
    public Integer setUserInfo(@RequestBody UserVo user) {
        User user1 = new User();
        user1.setAge(user.getAge());
        user1.setName("cazcen");

        log.info("user1:{}", user1);

        return 1;
    }

    @PostMapping("get/user/all/info")
    @ApiOperation("查询用户明细信息")
    public Result<PageData<UserDto>> getUserListInfo(@RequestBody UserVo vo) {

//        Assert.notNull(vo.getPhone(), "手机号不能为null");

        validatorParameter(vo);
        PageData<UserDto> userList = iUserService.getUserList(vo);
        Result<PageData<UserDto>> ok = Result.ok(userList);
        return ok;

    }


    @PostMapping("test/thread/call")
    @ApiOperation("测试多线程Callable")
    public void callableInfo() {

        ThreadUtilTest.doCallableB2();

    }

    @PostMapping("test/thread/run")
    @ApiOperation("测试多线程Runnable")
    public void runnableInfo() {

        ThreadUtilTest.doRunnableB2();

        log.info("我是主线程");

    }
}
